#include "Bipod.h"
#include <math.h>

// Material Properties
GLfloat bipod_diffuse[] = {0.7, 0.7, 0.7, 1.0};
GLfloat bipod_ambient[] = {0.7, 0.7, 0.7, 1.0};
GLfloat bipod_specular[] = {0.5, 0.5, 0.5, 1.0};

// Shininess
GLfloat bipod_shininess = 10.0f;

Bipod::Bipod(void)
{
	sizeFoot = 0.4;
	lenghtLegBipod = 1.0;
	sizeLegBipod = 0.05;
	sizeBaseBipod = 1.0;
	heightBaseBipod = 0.1;
	quad = gluNewQuadric();
	loadImage();
}

void Bipod::loadImage()
{
	Image* image1 = loadBMP("textures\\textureBipod.bmp");
	Image* image2 = loadBMP("textures\\textureBipod2.bmp");
	Image* image3 = loadBMP("textures\\textureFoot.bmp");
	Image* image4 = loadBMP("textures\\textureFoot2.bmp");
	Image* image5 = loadBMP("textures\\alu.bmp");
	Image* image6 = loadBMP("textures\\copper.bmp");
	textureBase1 = loadTexture(image1);
	textureBase2 = loadTexture(image2);
	textureFoot1 = loadTexture(image3);
	textureFoot2 = loadTexture(image4);
	textureLeg1 = loadTexture(image5);
	textureLeg2 = loadTexture(image6);

	currentTextureBase = textureBase1;
	currentTextureLeg = textureLeg1;
	currentTextureFoot = textureFoot1;

	delete image1;
	delete image2;
	delete image3;
	delete image5;
	delete image6;
}

void Bipod::switchTexture()
{
	if(currentTextureBase == textureBase1)
		currentTextureBase = textureBase2;
	else
		currentTextureBase = textureBase1;
	if(currentTextureLeg == textureLeg1)
		currentTextureLeg = textureLeg2;
	else
		currentTextureLeg = textureLeg1;
	if(currentTextureFoot == textureFoot1)
		currentTextureFoot = textureFoot2;
	else
		currentTextureFoot = textureFoot1;
}

GLuint Bipod::getTexture()
{
	return currentTextureBase;
}

void Bipod::display()
{
	GLfloat norm;
	glPushMatrix();
		glTranslatef(0.0, elevation, 0.0);

		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, bipod_diffuse);
		glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, bipod_ambient );
		glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, bipod_specular);
		glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, bipod_shininess);
		
		glColor3f(1,1,1);
		glTranslatef(0.0, lenghtLegBipod, 0.0);
		
		// BASE
		glPushMatrix();
			glBindTexture(GL_TEXTURE_2D, currentTextureBase);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			glColor3f(1.0f, 1.0f, 1.0f);
			
			glScalef(sizeBaseBipod, heightBaseBipod, sizeBaseBipod);
			drawCube(0.5,0.55);
		glPopMatrix();
		
		// LEG
		glPushMatrix();
			gluQuadricNormals(quad, GLU_SMOOTH);   // Create Smooth Normals (better for light)
			gluQuadricTexture(quad, GL_TRUE);      // Create Texture Coords
			for (int i=0; i!=2; ++i){
				glPushMatrix();
					glTranslatef(pow(-1.0, i)*(sizeBaseBipod/2 - sizeFoot/2), 0, 0);
					// UPPER LEG
					glPushMatrix();
						glBindTexture(GL_TEXTURE_2D, currentTextureLeg);
						glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
						glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
						glColor3f(1.0f, 1.0f, 1.0f);

						glPushMatrix();
							glTranslatef(0, 0.0, -2*sizeBaseBipod/5);
							glRotatef(60, 1.0, 0.0, 0.0);
							gluCylinder (quad, sizeLegBipod, sizeLegBipod, lenghtLegBipod, 10, 3);
						glPopMatrix();
						glPushMatrix();
							glTranslatef(0, 0.0, -sizeBaseBipod/5);
							glRotatef(60, 1.0, 0.0, 0.0);
							gluCylinder (quad, sizeLegBipod/2, sizeLegBipod/2, lenghtLegBipod, 10, 3);
						glPopMatrix();
					glPopMatrix();

					// FOOT
					glPushMatrix();
						glBindTexture(GL_TEXTURE_2D, currentTextureFoot);
						glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
						glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
						glColor3f(1.0f, 1.0f, 1.0f);

						glTranslatef(0, -(lenghtLegBipod - sizeFoot/3), sizeBaseBipod/5);
						glPushMatrix();
							glScalef(sizeFoot, 2*sizeFoot/3, sizeBaseBipod);
							drawCube(0.5, 0.635, 0.4, 0.67);
						glPopMatrix();
						glPushMatrix();
							glTranslatef(-sizeFoot/2, sizeFoot/3, sizeBaseBipod/2);
							glBegin(GL_QUADS);
								norm = sqrt(pow(sizeFoot*sizeBaseBipod/6,2)*pow(sizeFoot*2*sizeFoot/3,2));
								glNormal3f(0, (sizeFoot*sizeBaseBipod/6)/norm, (sizeFoot*2*sizeFoot/3)/norm);
								glTexCoord2f(0.0f, 0.635f); glVertex3f(0.0, 0.0, 0.0);
								glTexCoord2f(0.0f, 0.5f); glVertex3f(0.0, -2*sizeFoot/3, sizeBaseBipod/6);
								glTexCoord2f(0.4f, 0.5f); glVertex3f(sizeFoot, -2*sizeFoot/3, sizeBaseBipod/6);
								glTexCoord2f(0.4f, 0.635f); glVertex3f(sizeFoot, 0.0, 0.0);

								glNormal3f(0,-1,0);
								glTexCoord2f(0.0f, 0.635f); glVertex3f(0.0, -2*sizeFoot/3, 0.0);
								glTexCoord2f(0.4f, 0.635f); glVertex3f(sizeFoot, -2*sizeFoot/3, 0.0);
								glTexCoord2f(0.4f, 0.5f); glVertex3f(sizeFoot, -2*sizeFoot/3, sizeBaseBipod/6);
								glTexCoord2f(0.0f, 0.5f); glVertex3f(0.0, -2*sizeFoot/3, sizeBaseBipod/6);
							glEnd();
							glBegin(GL_TRIANGLES);
								glNormal3f(-1, 0,0);
								glTexCoord2f(0.5f, 1.0f); glVertex3f(0.0, 0.0, 0.0);
								glTexCoord2f(1.0f, 1.0f); glVertex3f(0.0, -2*sizeFoot/3, 0.0);
								glTexCoord2f(1.0f, 0.666f); glVertex3f(0.0, -2*sizeFoot/3, sizeBaseBipod/6);

								glNormal3f(1,0,0);
								glTexCoord2f(0.5f, 1.0f); glVertex3f(sizeFoot, 0.0, 0.0);
								glTexCoord2f(1.0f, 0.666f); glVertex3f(sizeFoot, -2*sizeFoot/3, sizeBaseBipod/6);
								glTexCoord2f(1.0f, 1.0f); glVertex3f(sizeFoot, -2*sizeFoot/3, 0.0);
							glEnd();
						glPopMatrix();
					glPopMatrix();
				glPopMatrix();
			}
		glPopMatrix();
	glPopMatrix();
}

float Bipod::getHeight(void)
{
	return (lenghtLegBipod + heightBaseBipod/2);
}

Bipod::~Bipod(void)
{
	gluDeleteQuadric(quad);
}